package cn.iocoder.yudao.adminserver.modules.live.service.projectPlan.impl;

import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.LiveProjectPlanCreateReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.LiveProjectPlanExportReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.LiveProjectPlanPageReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.LiveProjectPlanUpdateReqVO;
import cn.iocoder.yudao.adminserver.modules.live.convert.projectPlan.LiveProjectPlanConvert;
import cn.iocoder.yudao.adminserver.modules.live.dal.dataobject.projectPlan.LiveProjectPlanDO;
import cn.iocoder.yudao.adminserver.modules.live.dal.mysql.projectPlan.LiveProjectPlanMapper;
import cn.iocoder.yudao.adminserver.modules.live.service.projectPlan.LiveProjectPlanService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.adminserver.modules.tool.enums.ToolErrorCodeConstants.PROJECT_PLAN_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 项目阶段计划 Service 实现类
 *
 * @author 芋艿
 */
@Service
@Validated
public class LiveProjectPlanServiceImpl implements LiveProjectPlanService {

    @Resource
    private LiveProjectPlanMapper projectPlanMapper;

    @Override
    public Long createProjectPlan(LiveProjectPlanCreateReqVO createReqVO) {
        // 插入
        LiveProjectPlanDO projectPlan = LiveProjectPlanConvert.INSTANCE.convert(createReqVO);
        projectPlanMapper.insert(projectPlan);
        // 返回
        return projectPlan.getId();
    }

    @Override
    public void updateProjectPlan(LiveProjectPlanUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProjectPlanExists(updateReqVO.getId());
        // 更新
        LiveProjectPlanDO updateObj = LiveProjectPlanConvert.INSTANCE.convert(updateReqVO);
        projectPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectPlan(Long id) {
        // 校验存在
        this.validateProjectPlanExists(id);
        // 删除
        projectPlanMapper.deleteById(id);
    }

    private void validateProjectPlanExists(Long id) {
        if (projectPlanMapper.selectById(id) == null) {
            throw exception(PROJECT_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public LiveProjectPlanDO getProjectPlan(Long id) {
        return projectPlanMapper.selectById(id);
    }

    @Override
    public List<LiveProjectPlanDO> getProjectPlanList(Collection<Long> ids) {
        return projectPlanMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LiveProjectPlanDO> getProjectPlanPage(LiveProjectPlanPageReqVO pageReqVO) {
        return projectPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LiveProjectPlanDO> getProjectPlanList(LiveProjectPlanExportReqVO exportReqVO) {
        return projectPlanMapper.selectList(exportReqVO);
    }

}
